//
//  OMOrderDetailDelegater.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/15.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderDetailDelegater.h"

#import "OMOrderDetailResponse.h"
#import "MainConfigure.h"

#import "OMOrderInfoTableCell.h"
#import "OMOrderAddressTableCell.h"
#import "OMOrderProductsTableCell.h"
#import "OMOrderNOTableCell.h"

#import "WXPayConfig.h"
#import "OMOrder.h"
@interface OMOrderDetailDelegater()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray <NSArray <BaseTableCell*>*>*cellArray;
@property (nonatomic, strong) NSArray *cellHeights;
@property (nonatomic, strong) NSArray *sectionTitles;
@end
@implementation OMOrderDetailDelegater
- (instancetype)initWithTableView:(UITableView *)tableView
{
    self = [super init];
    _tableView = tableView;
    [self configure];
    return self;
}

- (void)configure
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

- (void)setResponse:(OMOrderDetailResponse *)response
{
    _response = response;
    [self reloadTableView];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [UILabel new];
    label.font = SM_CFont(27);
    label.textColor = SM_darkGrayColor;
    label.text = _sectionTitles[section];;
    return label;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cellArray[section].count;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return _cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _cellArray[indexPath.section][indexPath.row];
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return SM_SIZE(20);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SM_SIZE(50);
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [_cellHeights[indexPath.section][indexPath.row] floatValue];
}


- (void)reloadTableView
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


        CGFloat orderNoCellHeight = SM_SIZE(100);
        CGFloat addressCellHeight = [OMOrderAddressTableCell cellHeight];
        CGFloat productsCellHeight = [OMOrderProductsTableCell cellHeightWithProductNum:self.response.order.orderItems.count];
        _cellHeights = @[@[@(orderNoCellHeight)],@[@(addressCellHeight)],@[@(productsCellHeight)],@[@([OMOrderInfoTableCell cellHeight])],];
        _sectionTitles = @[@"    物流信息",@"    收货信息",@"    商品信息",@"    订单信息"];

        dispatch_async(dispatch_get_main_queue(), ^{
            OMOrderElementTableCell *deliveryCell = [OMOrderElementTableCell tableViewCellWithTableView:self.tableView];
            OMOrderAddressTableCell *addressCell = [OMOrderAddressTableCell tableViewCellWithTableView:self.tableView];
            OMOrderProductsTableCell *productsCell = [OMOrderProductsTableCell tableViewCellWithTableView:self.tableView];
            OMOrderInfoTableCell *infoCell = [OMOrderInfoTableCell tableViewCellWithTableView:self.tableView];
            _cellArray = @[@[deliveryCell],@[addressCell],@[productsCell],@[infoCell]];

            deliveryCell.elementLabel.text = @"配送方式";
            deliveryCell.detailLabel.text = self.response.order.shippingMethodName;
            [infoCell configureCellWithObj:self.response.order];
            [addressCell configureCellWithObj:self.response.order];
            [productsCell configureCellWithObj:self.response.order];
            [self.tableView reloadData];
        });
        
        
    });
    
}

@end
